//
//  YYLinkShareView.m
//  YYLinkShareView
//
//  Created by 云眼科技 on 16/5/26.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "YYLinkShareView.h"

CGFloat const closeBtnH   = 50.f;
CGFloat const lineHeight  = 0.5f;
CGFloat const shareBtnW   = 50.f;
CGFloat const whiteValue  = 0.7f;
CGFloat const alpha       = 0.7f;
CGFloat const btn_padding = 15.f;

NSInteger const maxItemNum = 5;

@implementation YYLinkPlatformBtn

-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    return CGRectMake(contentRect.size.width/4, 0, contentRect.size.width/2, contentRect.size.height *4/5 - 1.f); 
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0, contentRect.size.height *4/5 + 1.f , contentRect.size.width, contentRect.size.height/4 - 1.f);
}

@end


@interface YYLinkShareView (){
    NSArray      *_iconArray;
    NSDictionary *_titleDict;
    CGFloat       _baseViewH;
}
/** baseView */
@property (nonatomic, strong) UIView *baseView;
@end

@implementation YYLinkShareView

- (instancetype)initWithFrame:(CGRect)frame iconArray:(NSArray *)iconArray{
    self = [super initWithFrame:frame];
    if (self) {
        _baseViewH = [UIScreen mainScreen].bounds.size.height/3.f;
        _iconArray = iconArray;
        _titleDict = @{@"qq":@"QQ", @"qzone":@"QQ空间", @"weibo":@"微博", @"weixin":@"微信", @"circle":@"朋友圈"};
        [self setViewBackgroundColor];
        [self addTarget: self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


-(void)setViewBackgroundColor{
    self.backgroundColor = [UIColor colorWithWhite:whiteValue alpha:alpha];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self addSubview:self.baseView];
    //topLine
    [self.baseView addSubview:[self lineViewWithY:self.baseView.frame.size.height/5.f color:[UIColor greenColor]]];
    //label
    [self.baseView addSubview:[self labelWithTitle:@"分 享 至"]];
    //platformBtn
    [_iconArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *tempBtn = [self platformBtnWithFrame:[self calculateFrameWithIndex:idx] tag:idx];
        [self.baseView addSubview:tempBtn];
    }];
    //btnLine
    [self.baseView addSubview:[self lineViewWithY:self.baseView.frame.size.height*4/5.f color:[UIColor greenColor]]];
    //关闭按钮
    [self.baseView addSubview:[self closeButtonWithTarget:self action:@selector(closeView:)]];
}


-(CGRect)calculateFrameWithIndex:(NSUInteger)index{
    CGFloat btnW = (self.frame.size.width - btn_padding * maxItemNum) / (maxItemNum-1);
    CGFloat btnH = btnW;
    CGFloat btnX = btn_padding + index * (btnW+btn_padding);
    CGFloat btnY = self.baseView.frame.size.height/2.f - btnH/2.f;
    
    return CGRectMake(btnX, btnY, btnW, btnH);
}



#pragma mark -- ***************************** | 按钮响应方法 |  *****************************--

-(void)transformScale:(UIButton *)sender{
    [UIView animateWithDuration:0.3 animations:^{
        sender.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.7, 0.7);
    }];
}

-(void)shareBtnPressed:(UIButton *)sender{
    [UIView animateWithDuration:0.15 animations:^{
        sender.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectView:withPlatforType:)]) {
            [self.delegate didSelectView:self withPlatforType:[[YYLinkShareHandler defaultHandler] handlePlatformWithTitle:sender.titleLabel.text]];
            [self dismissShareViewAnimated:YES];
        }
    }];
}


-(void)closeView:(UIControl *)sender{
    [self dismissShareViewAnimated:YES];
}


#pragma mark -- ***************************** | show、dismiss |  *****************************--

-(void)showShareViewAnimated:(BOOL)animated{

    
    if (animated == YES) {
        [UIView animateWithDuration:0.3 animations:^{
            [self setViewBackgroundColor];
            [[UIApplication sharedApplication].keyWindow addSubview:self];
            self.baseView.frame = CGRectMake(0, self.frame.size.height - _baseViewH, self.frame.size.width, _baseViewH);
        } completion:^(BOOL finished) {
        }];
    }else{
        [self setViewBackgroundColor];
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.baseView.frame = CGRectMake(0, self.frame.size.height - _baseViewH, self.frame.size.width, _baseViewH);
    }
}


-(void)dismissShareViewAnimated:(BOOL)animated{
    if (animated == YES) {
        [UIView animateWithDuration:0.2 animations:^{
            self.baseView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, _baseViewH);
            self.backgroundColor = [UIColor colorWithWhite:whiteValue alpha:0];
        } completion:^(BOOL finished){
            [self removeFromSuperview];
        }];
    }else{
        self.baseView.frame = CGRectMake(0, self.frame.size.height - _baseViewH, self.frame.size.width, _baseViewH);
        [self removeFromSuperview];
    }
}





#pragma mark -- ***************************** | getter |  *****************************--

-(UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, _baseViewH)];
        _baseView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    }
    return _baseView;
}


#pragma mark -- ***************************** | views |  *****************************--

-(UIButton *)platformBtnWithFrame:(CGRect)frame tag:(NSInteger)tag{
    YYLinkPlatformBtn *tempBtn = [[YYLinkPlatformBtn alloc] init];
    tempBtn.frame = frame;
    [tempBtn setImage:[UIImage imageNamed:_iconArray[tag]]forState:UIControlStateNormal];
    NSString *titleKey = _iconArray[tag];
    NSString *titleValue = _titleDict[titleKey];
    [tempBtn setTitle:titleValue forState:UIControlStateNormal];
    [tempBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    tempBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    tempBtn.titleLabel.contentMode = UIViewContentModeScaleAspectFit;
    tempBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    tempBtn.titleLabel.font = [UIFont systemFontOfSize:tempBtn.titleLabel.frame.size.height*3.f/5.f];
    [tempBtn addTarget:self action:@selector(shareBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [tempBtn addTarget:self action:@selector(transformScale:) forControlEvents:UIControlEventTouchDown];

    return tempBtn;
}


-(UIView *)lineViewWithY:(CGFloat)Y color:(UIColor *)color{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, Y, self.frame.size.width, lineHeight)];
    lineView.backgroundColor = color;
    return lineView;
}



-(UILabel *)labelWithTitle:(NSString *)title{
    CGFloat labelH = self.baseView.frame.size.height/5.f;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, lineHeight, self.frame.size.width, labelH)];
    label.text = title;
    label.font = [UIFont italicSystemFontOfSize:labelH/2.f];
    label.textColor = [UIColor greenColor];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}


-(UIButton *)closeButtonWithTarget:(id)target action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, self.baseView.frame.size.height*4/5 - lineHeight, self.frame.size.width, self.baseView.frame.size.height/5.f);
    [btn setImage:[UIImage imageNamed:@"cancel_share"] forState:UIControlStateNormal];
    [btn addTarget: target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}


@end
